package com.mlm.mlmapp.activity;

import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.adapter.view.AQProgressDialog;
import com.mlm.mlmapp.adapter.view.CheckableButton;
import com.mlm.mlmapp.cons.HTTPCons;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.json.JSONArray;
import org.json.JSONException;

@EActivity(R.layout.city_list)
public class CityListActivity extends BaseActivity implements HTTPCons {
    @ViewById
    LinearLayout btn_back;
    @Extra
    String cityName;
    @Extra
    String localeCityName;
    @ViewById
    CheckableButton current_city;
    AQuery aq;
    private static int POST_VALUE = 1;
    //    @ViewById
//    FlowLayout flow_layout_city;
    @ViewById
    RadioGroup layout_city;
    boolean flag = false;

    @AfterViews
    public void init() {
        if (cityName != null && !cityName.equals("")) {
        } else {
            cityName = localeCityName;
        }

        current_city.setText(localeCityName);
        if (cityName.equals(localeCityName)) {
            current_city.setChecked(true);
        } else {
            current_city.setChecked(false);
        }
        current_city.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (current_city.isChecked())
                    return true;
                else
                    return false;
            }
        });
        current_city.setOnCheckedChangeWidgetListener(new CheckableButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CheckableButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Intent intent = new Intent();
                    intent.putExtra("cityName", buttonView.getText().toString());
                    CityListActivity.this.setResult(RESULT_OK, intent);
                    CityListActivity.this.finish();
                }
            }
        });
        ((TextView) this.findViewById(R.id.tv_title)).setText("当前城市-" + cityName





        );
        ((ImageButton) btn_back.getChildAt(0)).setImageResource(R.drawable.payment_close);
        aq = new AQuery(this);
        aq.progress(new AQProgressDialog(this)).ajax(get_dresserlist_hotcity_action, JSONArray.class,
                new AjaxCallback<JSONArray>() {

                    @Override
                    public void callback(String url, JSONArray object,
                                         AjaxStatus status) {
                        // TODO Auto-generated method stub
                        super.callback(url, object, status);
                        if (status.getCode() == -101) {
                            Toast.makeText(aq.getContext(), "网络异常,请检查网络",
                                    Toast.LENGTH_SHORT);
                            return;
                        }
                        if (status.getCode() != 200) {
                            Toast.makeText(aq.getContext(),
                                    getString(R.string.network_error),
                                    Toast.LENGTH_SHORT);
                        } else {
                            if (object.length() > 0) {
                                for (int i = 0; i < object.length(); i++) {
                                    try {
                                        addChildTo(layout_city,
                                                object.getString(i));

                                    } catch (JSONException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                                for (int i = 0; i < layout_city.getChildCount(); i++) {
                                    if (((RadioButton) layout_city.getChildAt(i)).getText().equals(cityName)) {
                                        ((RadioButton) layout_city.getChildAt(i)).setChecked(true);
                                    }
                                }
                            }
                        }
                    }

                });

    }

    @Click
    void btn_back() {
        Intent intent = new Intent();
        this.setResult(RESULT_CANCELED, intent);
        this.finish();
    }

    private void addChildTo(RadioGroup layout, final String json) {

        float density = getResources().getDisplayMetrics().density;
        RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int margin = (int) (10 * density);
        params.setMargins(0, 0, margin, 0);
        final RadioButton btn = new RadioButton(this);
        btn.setTextSize(14);
        btn.setTextColor(getResources().getColorStateList(R.color.checkable_text_color));
        btn.setBackgroundResource(R.drawable.checkable_background_normal);
        btn.setButtonDrawable(getResources().getDrawable(android.R.color.transparent));
        btn.setText(json);
//        if (json.equals(cityName)) {
//            btn.setChecked(true);
//        } else {
//            btn.setChecked(false);
//        }
        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                flag = true;
                return false;
            }
        });
        btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (flag) {
                        if (!localeCityName.equals(buttonView.getText().toString())) {
                            current_city.setChecked(false);
                        }
                        Intent intent = new Intent();
                        intent.putExtra("cityName", buttonView.getText().toString());
                        CityListActivity.this.setResult(RESULT_OK, intent);
                        CityListActivity.this.finish();
                    }
                }
            }
        });
        layout.addView(btn, params);
//        layout.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//
//            @Override
//            public void onCheckedChanged(RadioGroup arg0, int arg1) {
//                // TODO Auto-generated method stub
//                //获取变更后的选中项的ID
//
//
////                int radioButtonId = arg0.getCheckedRadioButtonId();
////                RadioButton rb = (RadioButton) CityListActivity.this.findViewById(radioButtonId);
//////                Toast.makeText(CityListActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
////                com.mlm.mlmapp.cons.Constants.CITY = rb.getText().toString();
////                CommonUtils.sendBroadCast(CityListActivity.this, HairDresserListActivity.CITY_REFRESH, "cityName",json);
////                CityListActivity.this.finish();
//            }
//        });

    }
}
